package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shenmazong.config.OssUploadTemplate;
import com.shenmazong.entity.TbTeach;
import com.shenmazong.mapper.TbTeachMapper;
import com.shenmazong.service.TbTeachService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.ListParamVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 教材表 ——> John
 服务实现类
 * </p>
 *
 * @author 还怕大雨吗
 * @since 2021-09-18
 */
@Service
public class TbTeachServiceImpl extends ServiceImpl<TbTeachMapper, TbTeach> implements TbTeachService {

    @Resource
    private TbTeachMapper tbTeachMapper;

    @Resource
    private OssUploadTemplate ossUploadTemplate;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /***
     * @description 查询大纲对应的教材
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/18 16:27
     */
    @Override
    @Cacheable(value = "selectTeach")
    public ResultResponse selectTeach(ListParamVo listParamVo) {
        IPage<TbTeach> list = tbTeachMapper.selectTeachPage(
                new Page<TbTeach>(listParamVo.getCurrent(),listParamVo.getSize()),
                 listParamVo.getParamMap().get("outlienID")
        );
        return ResultResponse.SUCCESS(list);
    }

    /***
     * @description 新增对应教材
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/18 16:32
     */
    @Override
    @CacheEvict(value = "selectTeach",allEntries = true)
    public ResultResponse insertTeach(TbTeach tbTeach) {
        try {
            tbTeachMapper.insert(tbTeach);
        } catch (Exception e) {
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 修改对应教材
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/18 16:33
     */
    @Override
    @CacheEvict(value = "selectTeach",allEntries = true)
    public ResultResponse updateTeach(TbTeach tbTeach) {
        tbTeachMapper.updateById(tbTeach);

        ObjectMapper objectMapper = new ObjectMapper();
        String teachInfo = null;
        try {
            teachInfo = objectMapper.writeValueAsString(tbTeach);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //发送消息
        rabbitTemplate.convertAndSend("TeachRabbit",teachInfo+"");
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 删除对应教材
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/18 16:35
     */
    @Override
    @CacheEvict(value = "selectTeach",allEntries = true)
    public ResultResponse deleteTeach(Long listParamVo) {
        tbTeachMapper.deleteById(listParamVo);
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 上传图片
     * @params [file]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/22 14:07
     */
    @Override
    public ResultResponse uploadTeachimages(MultipartFile file) {
        return ossUploadTemplate.uploadFileObjectStorage(file);
    }


}
